<?php

/**
* AdminAddCodeTest.php
*
* Guid add test
*
* @author     Blazej Zieba <b@zieba.info>
* @copyright  2007 konekto.com
* @version    CVS: $Id:$
*/
require_once '../framework/config.php';

require_once 'simpletest/autorun.php';
require_once 'simpletest/web_tester.php';

require_once '../framework/AuthSingleton.php';

require_once '../framework/Yt.class.php';

class  AdminAddCodeTest extends WebTestCase
{
    private $testName,$testDesc,$url;
    private $dbh = null ;
    private $username = '';
    private $password = '';

    public function setUp() 
    {
        $this->testGeocode = 'Poland';
        $this->username = md5( uniqid( rand(), true ) )."@pl.pl";
        $this->password = "zuza";
        
        $this->testName = md5(uniqid(rand(), true));
        $this->testDesc = md5(uniqid(rand(), true));
        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL');
        $this->url = rtrim($global['service_url'],'/');
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');    
        $this->dbh = new PDO($options['dsn_string']);
        $this->dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

        $this->addHeader("User-Agent: Mozilla/5.0(SymbianOS/9.2; U; Series60/3.1 NokiaE51/1.00.000; Profile/MIDP-2.0 Configuration/CLDC-1.1;)AppleWebKit/413(KHTML,like Gecko)Safari/413");

        $this->get($this->url.'?cmd=AdminAddCode');

        AuthSingleton::instance()->addUser($this->username,$this->password);
        $this->setFieldByName('username', $this->username);
        $this->setFieldByName('password', $this->password);
        $this->click("Login");

    
    }
    public function testAddDataToDatabaseNoRequired()
    {
        
        $this->assertTitle('Polowanie na kody Administracja');
        $this->setFieldByName("desc", $this->testDesc);
        $this->click("Zapisz");
        $this->assertText("Podaj nazwę");
    }

    public function testAddDataToDatabaseRequired()
    {

        $this->assertTitle('Polowanie na kody Administracja');
        $this->setFieldByName("name", $this->testName);
        $this->setFieldByName("desc", $this->testDesc);
        $this->click("Zapisz");
        $this->assertText("Kod dodany.");

        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
        $sth = $this->dbh->prepare("SELECT COUNT(guide.ROWID) 
            FROM guide,guide_name,guide_desc 
            WHERE guide_name.name = \"".$this->testName."\"
            AND guide_desc.desc = \"".$this->testDesc."\"
            AND guide.code IS NOT NULL 
            AND length(guide.code) = 6
            AND guide.rowid = guide_name.guide_id");
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],1);
    }

    public function testAddDataToDatabaseRequiredAndFoto()
    {


        $this->assertTitle('Polowanie na kody Administracja');
        $this->setFieldByName("name", $this->testName);
        $this->setFieldByName("desc", $this->testDesc);
            
        $this->assertTrue($this->setFieldByName('pic_file', dirname(__FILE__).'/image.jpg'));
        $this->assertFieldByName('pic_file', dirname(__FILE__).'/image.jpg');

        $this->click("Zapisz");
        
        $this->assertText("Kod dodany.");

        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
        $this->dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
        $sth = $this->dbh->prepare("SELECT COUNT(guide.ROWID) 
            FROM guide,guide_name,guide_desc 
            WHERE guide_name.name = \"".$this->testName."\"
            AND guide_desc.desc = \"".$this->testDesc."\"
            AND guide.code IS NOT NULL 
            AND length(guide.code) = 6
            AND guide.rowid = guide_name.guide_id
            AND guide.pic_file  LIKE \"%image.jpg\"");
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],1);
                        
    }

    public function testAddDataToDatabaseRequiredAndFotoWithWrongExtension()
    {

        $this->assertTitle('Polowanie na kody Administracja');
        $this->setFieldByName("name", $this->testName);
        $this->setFieldByName("desc", $this->testDesc);
            
        $this->assertTrue($this->setFieldByName('pic_file', dirname(__FILE__).'/fake.txt'));
        $this->assertFieldByName('pic_file', dirname(__FILE__).'/fake.txt');

        $this->click("Zapisz");

        $this->assertText("Niepoprawny format pliku");
                
    }


    public function testAddDataToDatabasAll()
    {
        $this->assertTitle('Polowanie na kody Administracja');
        $this->setFieldByName("name", $this->testName);
        $this->setFieldByName("desc", $this->testDesc);
        $this->setFieldByName("geocode", $this->testGeocode);
        $this->setFieldByName("publish", 1); 
            

        $this->assertTrue($this->setFieldByName('pic_file', dirname(__FILE__).'/image.jpg'));
        $this->assertFieldByName('pic_file', dirname(__FILE__).'/image.jpg');
        $this->assertTrue($this->setFieldByName('video_file', dirname(__FILE__).'/video.3gp'));
        $this->assertFieldByName('video_file', dirname(__FILE__).'/video.3gp');

        $this->setConnectionTimeout(30);
        $this->click("Zapisz");
  
        $this->assertText("Kod dodany.");
        
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
        $sth = $this->dbh->prepare("SELECT COUNT(guide.ROWID) 
            FROM guide,guide_name,guide_desc 
            WHERE guide_name.name = \"".$this->testName."\"
            AND guide_desc.desc = \"".$this->testDesc."\"
            AND guide.code IS NOT NULL 
            AND length(guide.code) = 6
            AND guide.rowid = guide_name.guide_id
            AND guide.pic_file  LIKE \"%image.jpg\"
            AND guide.video_file  NOT NULL");

        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],1);
      
    }
    
    public function testFileSizeSet()
    {

        $this->assertTitle('Polowanie na kody Administracja');
        $this->assertField('MAX_FILE_SIZE',"2097152");
    }

    public function testAddDataToDatabaseRequiredAndVideo()
    {

        $this->assertTitle('Polowanie na kody Administracja');
        $this->setFieldByName("name", $this->testName);
        $this->setFieldByName("desc", $this->testDesc);
        $this->setFieldByName("publish", 1);            

        $this->assertTrue($this->setFieldByName('video_file', dirname(__FILE__).'/video.3gp'));
        $this->assertFieldByName('video_file', dirname(__FILE__).'/video.3gp');

        $this->setConnectionTimeout(30);
        $this->click("Zapisz");

        $this->assertText("Kod dodany.");

        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  

        $sth = $this->dbh->prepare("SELECT guide.video_file 
            FROM guide,guide_name,guide_desc 
            WHERE guide_name.name = \"".$this->testName."\"
            AND guide_desc.desc = \"".$this->testDesc."\"
            AND guide.code IS NOT NULL 
            AND length(guide.code) = 6
            AND guide.rowid = guide_name.guide_id
            AND guide.video_file NOT NULL");
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);

        $ch = curl_init();
         
        curl_setopt($ch, CURLOPT_URL, "http://www.youtube.com/watch?hl=pl&v=".$result['video_file']);
        curl_setopt($ch, CURLOPT_HEADER,1);
        curl_setopt($ch, CURLOPT_USERAGENT, isset($_SERVER["HTTP_USER_AGENT"]) ? $_SERVER["HTTP_USER_AGENT"] : null );
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0); 
        $options = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL'); 
        curl_setopt($ch, CURLOPT_PROXY,  isset($options['proxy']) ? $options['proxy'] : null ); 
        $data = curl_exec($ch);
        curl_close($ch);
        //wait a while to avoid google too many calls error
        sleep(1);
  
        $this->assertTrue(strpos($data,"Ten film wideo nie został jeszcze przetworzony."));


    }

 
    public function testAddDataToDatabaseRequiredAndVideoDontPublish()
    {

        $this->assertTitle('Polowanie na kody Administracja');
        $this->setFieldByName("name", $this->testName);
        $this->setFieldByName("desc", $this->testDesc);
        $this->setFieldByName("publish", 0);
            

        $this->assertTrue($this->setFieldByName('video_file', dirname(__FILE__).'/video.3gp'));
        $this->assertFieldByName('video_file', dirname(__FILE__).'/video.3gp');

        $this->setConnectionTimeout(30);
        $this->click("Zapisz");

        $this->assertText("Kod dodany.");

        sleep(15);//Wideo processing
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  

        $sth = $this->dbh->prepare("SELECT guide.video_file 
            FROM guide,guide_name,guide_desc 
            WHERE guide_name.name = \"".$this->testName."\"
            AND guide_desc.desc = \"".$this->testDesc."\"
            AND guide.code IS NOT NULL 
            AND length(guide.code) = 6
            AND guide.rowid = guide_name.guide_id
            AND guide.video_file NOT NULL");
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $ch = curl_init();
         
        curl_setopt($ch, CURLOPT_URL, "http://www.youtube.com/watch?hl=pl&v=".$result['video_file']);
        curl_setopt($ch, CURLOPT_HEADER,1);
        curl_setopt($ch, CURLOPT_USERAGENT, isset($_SERVER["HTTP_USER_AGENT"]) ? $_SERVER["HTTP_USER_AGENT"] : null );
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0); 
        $options = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL'); 
        curl_setopt($ch, CURLOPT_PROXY,  isset($options['proxy']) ? $options['proxy'] : null ); 
        $data = curl_exec($ch);
        curl_close($ch);
        //wait a while to avoid google too many calls error
        sleep(1);
        $this->assertTrue(strpos($data,"ServiceLogin"));


    }


    public function testAddDataToDatabaseRequiredAndVideoWithWrongFileExtension()
    {

        $this->assertTitle('Polowanie na kody Administracja');
        $this->setFieldByName("name", $this->testName);
        $this->setFieldByName("desc", $this->testDesc);
            

        $this->assertTrue($this->setFieldByName('video_file', dirname(__FILE__).'/fake.txt'));
        $this->assertFieldByName('video_file', dirname(__FILE__).'/fake.txt');

        $this->click("Zapisz");

        $this->assertText("Niepoprawny format pliku");
    }
    public function testResolveGeocode()
    {
        $location = "poland";
        $this->assertTitle('Polowanie na kody Administracja');
        $this->setFieldByName("name", $this->testName);
        $this->setFieldByName("geocode", $location);    

        $this->click("Zapisz");

        $this->assertText("Kod dodany.");

        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
  
        $this->dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

        $sth = $this->dbh->prepare("SELECT COUNT(guide.ROWID) FROM guide,guide_name 
                                    WHERE guide.geocode = :geocode 
                                    AND guide_name.name = :name
                                    AND guide.rowid = guide_name.guide_id");
        
        $sth->bindParam(":name",$this->testName,PDO::PARAM_STR);
        $sth->bindParam(":geocode",$location,PDO::PARAM_STR);
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],1); 

    }
    public function testDoNotResolveGeocode()
    {
        $location = "MagicPlaceLocatedOnMars";
        $latitude = "" ;
        $longitude = "";
        $this->assertTitle('Polowanie na kody Administracja');
        $this->setFieldByName("name", $this->testName);
        $this->setFieldByName("geocode", $location);    

        $this->click("Zapisz");

        $this->assertText("Kod dodany.");

        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
  
        $this->dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

        $sth = $this->dbh->prepare("SELECT COUNT(guide.ROWID) FROM guide,guide_name 
                                    WHERE guide.geocode = :geocode 
                                    AND guide_name.name = :name
                                    AND guide.rowid = guide_name.guide_id");
        
        $sth->bindParam(":name",$this->testName,PDO::PARAM_STR);
        $sth->bindParam(":geocode",$location,PDO::PARAM_STR);
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],1); 

    }

    public function testAddDataToDatabaseRequiredAndPublish()
    {

        $this->assertTitle('Polowanie na kody Administracja');
        $this->setFieldByName("name", $this->testName);
        $this->setFieldByName("desc", $this->testDesc);
        $this->setFieldByName("publish",1);
        $this->click("Zapisz");
        $this->assertText("Kod dodany.");

        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
        $sth = $this->dbh->prepare("SELECT COUNT(guide.ROWID) 
            FROM guide,guide_name,guide_desc 
            WHERE guide_name.name = \"".$this->testName."\"
            AND guide_desc.desc = \"".$this->testDesc."\"
            AND guide.code IS NOT NULL 
            AND length(guide.code) = 6
            AND guide.rowid = guide_name.guide_id
            AND guide.publish = 1");
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],1);
        
    }

    public function testAddDataToDatabaseRequiredAndDontPublish()
    {

        $this->assertTitle('Polowanie na kody Administracja');
        $this->setFieldByName("name", $this->testName);
        $this->setFieldByName("desc", $this->testDesc);
        $this->setFieldByName("publish",0);
        $this->click("Zapisz");
        $this->assertText("Kod dodany.");

        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
        $sth = $this->dbh->prepare("SELECT COUNT(guide.ROWID) 
            FROM guide,guide_name,guide_desc 
            WHERE guide_name.name = \"".$this->testName."\"
            AND guide_desc.desc = \"".$this->testDesc."\"
            AND guide.code IS NOT NULL 
            AND length(guide.code) = 6
            AND guide.rowid = guide_name.guide_id
            AND guide.publish = ''");
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],1);
        
    }   

    public function testAddVideoFromIdAndFile()
    {
        $this->assertTitle('Polowanie na kody Administracja');
        $this->setFieldByName("name", $this->testName);
        $this->setFieldByName("desc", $this->testDesc);
        
        $this->assertTrue($this->setFieldByName('video_file', dirname(__FILE__).'/video.3gp'));
        $this->assertFieldByName('video_file', dirname(__FILE__).'/video.3gp');

        $this->setFieldByName("yt_id", "test");

        $this->click("Zapisz");
        $this->assertText("Podaj plik lub id");
    }
    
    public function testAddVideoWithWrongYtHref()
    {

        $this->assertTitle('Polowanie na kody Administracja');
        $this->setFieldByName("name", $this->testName);
        $this->setFieldByName("desc", $this->testDesc);
        

        $this->setFieldByName("yt_id", "test");

        $this->click("Zapisz");
        $this->assertText("Nieprawidłowy id");

    }

    public function testAddVideoWithYtHrefOk()
    {
        $this->assertTitle('Polowanie na kody Administracja');
        $this->setFieldByName("name", $this->testName);
        $this->setFieldByName("desc", $this->testDesc);
        

        $this->setFieldByName("yt_id", "jNQXAC9IVRw");

        $this->click("Zapisz");
        $this->assertText("Kod dodany.");
    }
    public function tearDown() 
    {
        
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
  
        $sth = $this->dbh->prepare("SELECT guide.pic_file,guide.video_file,guide.rowid 
                                    FROM guide,guide_name
                                    WHERE guide_name.name = \"".$this->testName."\" 
                                    AND guide.rowid = guide_name.guide_id");
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_ASSOC);
        
        
        if ( is_file($upload['upload_dir'].$result['pic_file']) )
        {
            unlink($upload['upload_dir'].$result['pic_file']);
        }


        if ( $result['video_file'] != '' )
        {
            $yt = Yt::getClient();
            try
            {
                $videoEntry = $yt->getVideoEntry($result['video_file'],null,true);
                $yt->delete($videoEntry);
            }
            catch(Zend_Gdata_App_HttpException $e)
            {
            }
        }

        //if code was added delete translations of name and descryption
        if ( $result['rowid'] > 0 )
        {
    
            $sth = $this->dbh->prepare("DELETE FROM guide_name WHERE guide_id = ".$result['rowid']);
            $sth->execute();

            $sth = $this->dbh->prepare("DELETE FROM guide_desc WHERE guide_id = ".$result['rowid']);
            $sth->execute();        
        }

        //Delete all code beleongs to username
        $sth = $this->dbh->prepare("DELETE FROM guide WHERE owner = \"".$this->username."\"");
        $sth->execute();

        AuthSingleton::instance()->removeUser($this->username);
        $this->get($this->url.'?cmd=AdminAddCode&logout=1');
    }
    
}
?>
